package com.zthz.douxing.command

import com.zthz.douxing.Person
import com.zthz.douxing.utils.ValidateConstanst
import grails.validation.Validateable
import redis.clients.jedis.Jedis

/**
 * Created by gengjet on 3/12/14.
 */
@Validateable
class DouXingCommand {
    String uid
    String vid
    String pid // ios 为0 android 为 1， wphone为3
    String cid
    String v
    String xgToken //信鸽token
}

@Validateable
class SendSmsCode extends DouXingCommand {

    String mobileNo
    String xgToken

    static constraints = {
        xgToken blank: false, nullable: false
        mobileNo nullable: false, blank: false
    }
}

@Validateable
class SendGeTuiSmsCode extends DouXingCommand {

    String mobileNo
    String getuiId

    static constraints = {
        getuiId blank: false, nullable: false
        mobileNo nullable: false, blank: false
    }
}

class SMSCodeCommand extends DouXingCommand {

    String smsCode
    String mobileNo
    def redisService

    static constraints = {
        xgToken blank: false, nullable: false
        smsCode blank: false, validator: {
            value, command ->
                command.redisService.withRedis {
                    Jedis redis ->
                        if (!redis.get("SMS_${command.mobileNo}").equals(value))
                            return 'command.errorRandomCode'
                }
        }
    }
}

@Validateable
class DouXingRegisterCommand extends SMSCodeCommand {

    def redisService

    String email
    String password
    String password2
    String mobileNo
    String username

    String xgToken
    String getuiId

    static constraints = {

        password blank: false, nullable: false, validator: ValidateConstanst.password

        password2 blank: false, nullable: false, validator: ValidateConstanst.password2

        email blank: true, email: true, nullable: true

        xgToken blank: true, nullable: true

        getuiId blank: true, nullable: true

        mobileNo blank: false, nullable: false, validator: {
                //判断有没有被注册
            value, command ->

                if (Person.findByMobileNo(value) != null) {

                    return "douXingRegisterCommand.hasMobileNo"
                }
        }

        username blank: false, nullable: false, validator: {

            value, command ->

                if (Person.findByUsername(value) != null) {
                    return "command.existUser"
                }
        }

    }
}
/**
 *
 *
 * @desc 忘记密码（不包括邮箱的）
 * @author zsw
 * @date Mar 20, 2014
 */
@Validateable
class FrogotPasswordCommand extends SMSCodeCommand {

    String username

    String password

    String password2

    String mobileNo;

    static constraints = {

        username blank: false, nullable: true

        mobileNo blank: false, pattern: ValidateConstanst.MobileNoPattern

        password blank: false, nullable: false, validator: ValidateConstanst.password

        password2 blank: false, nullable: false, validator: ValidateConstanst.password2
    }
}

@Validateable
class DouXingQueryPlanCommand extends DouXingCommand {
    String saveType

    static constraints = {
        saveType(blank: false, nullable: false)
    }
}

@Validateable
class QueryPagerCommand extends DouXingCommand {
    String max
    String offset

    static constraints = {
        max(blank: true, nullable: true)
        offset(blank: true, nullable: true)
    }
}

@Validateable
class DouXingActionByPlanTripIdCommand extends DouXingCommand {
    String planTripId

    static constraints = {
        planTripId(blank: false, nullable: false)
    }
}

@Validateable
class DataCompleteByWordCommand extends DouXingCommand {
    String word

    static constraints = {
        word(blank: false, nullable: false)
    }
}

@Validateable
class UpdateSortIndexCommand extends DouXingCommand {
    Integer key
    String staName

    static constraints = {
        key(blank: false, nullable: false)
        staName(blank: false, nullable: false)
    }
}

@Validateable
class DouXingRestPasswordCommand extends DouXingCommand {

    String username

    String oldPassword
    String password2
    String password

    static constraints = {

        username blank: true, nullable: true

        oldPassword blank: false, nullable: false


        password blank: false, nullable: false, validator: ValidateConstanst.password

        password2 blank: false, nullable: false, validator: ValidateConstanst.password2
    }
}

@Validateable
class DouXingGetRandomCodeCommand extends DouXingCommand {

    String email;
    String mobileNo;
    static constraints = {
        mobileNo(blank: false, matches: ValidateConstanst.MobileNoPattern)
    }
}
/**
 *
 * @desc 用户信息更新命令
 */
@Validateable
class DouXingUserUpdateCommand extends DouXingCommand {

    String realName
    String idType
    String idNo
    String email
    String username
    String mobileNo
    String commonTravelWay
    String iconUrl

    def springSecurityService

    static constraints = {

        mobileNo blank: true, validator: ValidateConstanst.MobileNo, nullable: true

        idType blank: true, nullable: true, validator: ValidateConstanst.IDTYPE

        idNo blank: true, pattern: ValidateConstanst.IdNoPattern, nullable: true

        email blank: true, email: true, nullable: true

        realName blank: true, nullable: true

        commonTravelWay blank: true, nullable: true

        username blank: true, nullable: true, validator: {

            value, command ->

                def newPerson = Person.findByUsername(value.replace(" ", ""))

                def oldPerson = null

                if (command.springSecurityService.loggedIn) {

                    oldPerson = Person.get(command.springSecurityService.principal.id)

                    if (!oldPerson.username.equals(value)) {

                        if (newPerson)
                            return 'command.existUser'
                    }
                } else {

                    return "command.noUser"
                }
        }
        iconUrl blank: true, nullable: true

    }


}
